// 转换登录方式
let oitem = document.getElementsByClassName("nav-item");
// 登录内容
let oBox = document.querySelector(".c-tab-con");
// 两种登录方式
let ocon = document.getElementsByClassName("con");
for (let i = 0; i < oitem.length; i++) {
    //创建新的属性
    oitem[i].index = i;
    oitem[i].onclick = function () {
        for (var j = 0; j < oitem.length; j++) {
            //this只对应当前时间的dom元素
            if (this.index == j) {
                oitem[j].id = "pick";
            } else {
                oitem[j].id = "nopick";
            }
        }
        switch (this.index) {
            case 0:
                ocon[0].style.display = "block";
                ocon[1].style.display = "none";
                break;
            case 1:
                ocon[1].style.display = "block";
                ocon[0].style.display = "none";
                break;
        }
    }
}

// 登录界面
let oBtnLogin = document.getElementById("btnLogin");
let oUserPass = document.getElementById("userpass");
let oUserName = document.getElementById("username");

oBtnLogin.onclick = function () {
    let xhr = new XMLHttpRequest();
    xhr.open("post", "./php/login.php", true);

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            console.log(xhr.responseText)
            if (xhr.responseText == "﻿success") {
                document.getElementById("usertip").innerHTML = "登录成功";
                saveCookie("vipName", oUserName.value, 1)
                location.href = "http://127.0.0.1/test/VIP/index.html"
            } else {
                document.getElementById("usertip").innerHTML = "登录失败,用户名或者密码错误";
            }
        }
    }

    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    let str = "username=" + oUserName.value + "&userpass=" + oUserPass.value;
    xhr.send(str);

}
